import { Injectable } from '@angular/core';
import {Http,Response} from '@angular/http'
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch'

/*
  Generated class for the RestProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class RestProvider {

  constructor(public http: Http) {
    //console.log('Hello RestProvider Provider');
  }

  //feed
  private apiUrlFeeds = 'https://imoocqa.gugujiankong.com/api/feeds/get';

  //account
  private apiUrlRegister = 'https://imoocqa.gugujiankong.com/api/account/register';
  private apiUrlLogin = 'https://imoocqa.gugujiankong.com/api/account/login';
  private apiUrlUserInfo = 'https://imoocqa.gugujiankong.com/api/account/userinfo';
  private apiUrlUpdateNickName = 'https://imoocqa.gugujiankong.com/api/account/updatenickname';
  private apiGetUserQuestionList = "https://imoocqa.gugujiankong.com/api/account/getuserquestionlist";

  //question
  private apiUrlQuestionSave = 'https://imoocqa.gugujiankong.com/api/question/save';
  private apiUrlQuestionList = 'https://imoocqa.gugujiankong.com/api/question/list?index=1&number=10';
  private apiUrlGetQuestion = "https://imoocqa.gugujiankong.com/api/question/get";
  private apiUrlGetQuestionWithUser = "https://imoocqa.gugujiankong.com/api/question/getwithuser";
  private apiUrlAnswer = "https://imoocqa.gugujiankong.com/api/question/answer";
  private apiUrlSaveFavourite = "https://imoocqa.gugujiankong.com/api/question/savefavourite";

  //notification
  private apiUrlUserNotifications = "https://imoocqa.gugujiankong.com/api/account/usernotifications";



  /**
   *登录
   *
   * @param {*} mobile
   * @param {*} password
   * @returns {Observable<string[]>}
   * @memberof RestProvider
   */
  login(mobile,password):Observable<string[]>{
    return this.getUrlReturn(this.apiUrlLogin +"?mobile="  +mobile + "&password=" + password);

  }


/**
 *注册
 *
 * @param {*} mobile
 * @param {*} nickname
 * @param {*} password
 * @returns {Observable<string[]>}
 * @memberof RestProvider
 */
  register(mobile,nickname,password):Observable<string[]>{
    return this.getUrlReturn(this.apiUrlRegister+ "?mobile=" + mobile +"&nickname=" + nickname + "&password=" + password);
  }

  getUserInfo(userId):Observable<string[]>{
    return this.getUrlReturn(this.apiUrlUserInfo + "?userid=" + userId);
  }

  updateNickName(userId,nickname):Observable<string[]> {
    return this.getUrlReturn(this.apiUrlUpdateNickName + "?userid=" + userId + "&nickname=" + nickname)
    
  }

  saveQuestion(userId,title,content):Observable<string[]> {
    return this.getUrlReturn(this.apiUrlQuestionSave+"?userid="+userId+"&title="+title+"&content="+content)
  }

  getFeeds():Observable<string[]>{
    return this.getUrlReturn(this.apiUrlFeeds);

  }

   /**
   * 获取用户的提醒消息
   * 
   * @param {any} userId 
   * @returns {Observable<string[]>} 
   * @memberof RestProvider
   */
  getUserNotifications(userId): Observable<string[]> {
    return this.getUrlReturn(this.apiUrlUserNotifications + "?userid=" + userId);
  }

  /**
   *
   *
   * @memberof RestProvider
   */
  answer(userId,questionId,content){
    return this.getUrlReturn(this.apiUrlAnswer + "?userid=" + userId + "&questionid=" + questionId + "&content=" + content);

    }
  /**
   *获取问题详情
  *
  * @param {*} id
  * @returns {Observable<string[]>}
  * @memberof RestProvider
  */
  getQuestion(id):Observable<string[]>{
      return this.getUrlReturn(this.apiUrlGetQuestion+"?id="+id)
    }

  getQuestions():Observable<string[]>{
    return this.getUrlReturn(this.apiUrlQuestionList)
    }  
  /**
   *根据userid 查询该用户是否关注此问题
   *
   * @param {*} id
   * @returns {Observable<string[]>}
   * @memberof RestProvider
   */
  getQuestionWithUser(questionId,userId):Observable<string[]>{
      return this.getUrlReturn(this.apiUrlGetQuestionWithUser+"?id=" + questionId + "&userid=" + userId);
    }

  /**
   *获取用户的相关问题列表
   *
   * @param {*} userId
   * @param {*} type question/answer/favourite
   * @returns {Observable<string[]>}
   * @memberof RestProvider
   */
  getUserQuestionList(userId,type):Observable<string[]>{
    return this.getUrlReturn(this.apiGetUserQuestionList + "?userid=" + userId + "&type=" + type);

  }

  saveFavourite(questionId,userId):Observable<string[]>{
    return this.getUrlReturn(this.apiUrlSaveFavourite+"?questionid=" + questionId + "&userid=" + userId);
  }

  /**
   *全局获取http请求的方法
   *
   * @private
   * @param {string} url
   * @returns {Observable<string[]>}
   * @memberof RestProvider
   */
  private getUrlReturn(url:string): Observable<string[]>{
    return this.http.get(url)
                    .map(this.extractData)
                    .catch(this.handleError)


  }
  /**
   *处理接口返回的数据（json格式）
   *
   * @private
   * @param {Response} res
   * @returns
   * @memberof RestProvider
   */
  private extractData (res:Response){
    let body = res.json();
    return JSON.parse(body) || {};

  }

  /**
   *错误处理
   *
   * @private
   * @param {(Response | any)} error
   * @returns
   * @memberof RestProvider
   */
  private handleError (error:Response | any ){
    let errMsg:string;
    if (error instanceof Response){
      const body = error.json() || "";
      const err = body.error || JSON.parse(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`
       }else{
         errMsg = error.message ? error.message : error.toString();
       }
      console.error(errMsg)
      return Observable.throw(errMsg)

  }
  

}
